package leetcode每日一题;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 1. 问题描述
 *
 * 2. 算法分析
 *
 * 3. 代码实现
 */
@SuppressWarnings("all")
public class 两数字相加_链表形式存储的数字 {
    static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        /**
         * 暴力法
         *
         * @param l1
         * @param l2
         * @return
         */
        public static ListNode addTwoNumbers1(ListNode l1, ListNode l2) {
            ListNode res = new ListNode();
            ListNode cur = res;
            int reminder = 0;
            int next = 0;
            while (l1 != null || l2 != null) {
                reminder = next;
                if (l1 != null) {
                    reminder += l1.val;
                    next += l1.val;
                    l1 = l1.next;
                }
                if (l2 != null) {
                    reminder += l2.val;
                    next += l2.val;
                    l2 = l2.next;
                }
                reminder %= 10;
                next /= 10;
                cur.next = new ListNode(reminder);
                cur = cur.next;
            }
            if (next != 0) {
                cur.next = new ListNode(next);
                cur = cur.next;
            }
            return res.next;
        }

        /**
         * 递归法
         * @param l1
         * @param l2
         * @return
         */
        public static ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
            int sum = l1.val + l2.val;
            int next = sum / 10;
            ListNode res = new ListNode(sum % 10);
            if(l1.next != null || l2.next != null || next != 0) {
                l1 = (l1.next != null) ? l1.next : new ListNode(0);
                l2 = (l2.next != null) ? l2.next : new ListNode(0);
                l1.val += next;
                res.next = addTwoNumbers2(l1,l2);
            }
            return res;
        }
    }

    public static void main(String[] args) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> a = new ArrayList<>(); a.add(0); a.add(1);
        List<Integer> b = new ArrayList<>(); b.add(0); b.add(1);
        res.add(a); res.add(b);
        System.out.println(res.get(0).contains(1));
    }
}
